package com.taojin.shoppinginterest.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.taojin.shoppinginterest.R;
import com.taojin.shoppinginterest.bean.SpecialList;
import com.taojin.shoppinginterest.bean.Sub;
import com.taojin.shoppinginterest.utils.BitmapHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by T400 on 15-5-11.
 */
public class SubjectFragmentAdapter extends AppAdapter {
    //child的数据源
    //private List<SpecialList> list;
    private List<Sub> itemSub;
    private BitmapUtils bitmapUtils;
    public SubjectFragmentAdapter(List<SpecialList> list,Context context) {
        super(list, context);
        getItems(list);
        bitmapUtils= BitmapHelper.getInstance().getBitmapUtils(context,R.mipmap.icon,R.mipmap.icon);
    }

    public void getItems(List<SpecialList> list) {
        itemSub = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            List<Sub> specialList = list.get(i).getSub();
            for (int j = 0; j < specialList.size(); j++) {
                Sub s = specialList.get(j);
                if (j == 0) {
                    s.time = list.get(i).getTime();
                    s.isShowTitle = true;
                }
                itemSub.add(s);
            }
        }
    }

    @Override
    public int getCount() {
        return
                itemSub.size();
    }

    @Override
    public Object getItem(int position) {
        return itemSub.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View createItemView(int position, View convertView, ViewGroup parent) {

        Sub s = itemSub.get(position);
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder(convertView);
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_home_special_child_item, null);
            if (s.isShowTitle) {
                viewHolder.tvTime.setVisibility(TextView.VISIBLE);
                viewHolder.tvTime.setText(s.time);
            }
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tvAdvice.setText(itemSub.get(position).getTitle());
        viewHolder.tvName.setText(itemSub.get(position).getCate());
        viewHolder.tvBrowser.setText(itemSub.get(position).getView_num());
        bitmapUtils.display(viewHolder.imageViewSubject, itemSub.get(position).getImg_url());
        return convertView;
    }

    public class ViewHolder {


        @ViewInject(R.id.fragment_home_special_child_item_time)
        private TextView tvTime;
        @ViewInject(R.id.fragment_home_special_child_item_advice)
        private TextView tvAdvice;
        @ViewInject(R.id.fragment_home_special_child_item_image)
        private ImageView imageViewSubject;
        @ViewInject(R.id.fragment_home_special_child_item_name)
        private TextView tvName;
        @ViewInject(R.id.fragment_home_special_child_item_browse)
        private TextView tvBrowser;

        public ViewHolder(View convertView) {
            ViewUtils.inject(this, convertView);
        }
    }

}
